package com.huaxin.hxmoduleworkorder.mapping;

import com.huaxin.hxmoduleworkorder.common.enums.*;
import com.huaxin.hxmoduleworkorder.dto.request.MaintenanceOrderCreateReqDTO;
import com.huaxin.hxmoduleworkorder.dto.request.MaintenanceOrderUpdateReqDTO;
import com.huaxin.hxmoduleworkorder.models.MaintenanceOrder;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

@Mapper(componentModel = "spring")
public interface MaintenanceOrderMapping {
    @Mapping(target = "type", ignore = true)
    @Mapping(target = "priority", ignore = true)
    @Mapping(target = "sourceType", ignore = true)
    @Mapping(target = "customerType", ignore = true)
    @Mapping(target = "createdByRole", ignore = true)
    MaintenanceOrder toEntity(MaintenanceOrderCreateReqDTO in);

    @AfterMapping
    default void afterMapping(MaintenanceOrderCreateReqDTO in, @MappingTarget MaintenanceOrder entity) {
        entity.setStatus(MaintenanceOrderStatus.CREATED);
        entity.setType(OrderType.of(in.getType()));
        entity.setPriority(MaintenanceOrderPriority.of(in.getPriority()));
        entity.setSourceType(MaintenanceOrderSourceType.of(in.getSourceType()));
        entity.setCustomerType(CustomerType.of(in.getCustomerType()));
        entity.setCreatedByRole(MaintenanceOrderOperatorRole.of(in.getCreatedByRole()));
    }

    @Mapping(target = "priority", ignore = true)
    @Mapping(target = "sourceType", ignore = true)
    @Mapping(target = "customerType", ignore = true)
    void updateEntity(MaintenanceOrderUpdateReqDTO in, @MappingTarget MaintenanceOrder entity);

    @AfterMapping
    default void afterMapping(MaintenanceOrderUpdateReqDTO in, MaintenanceOrder entity) {
        entity.setPriority(MaintenanceOrderPriority.of(in.getPriority()));
        entity.setSourceType(MaintenanceOrderSourceType.of(in.getSourceType()));
        entity.setCustomerType(CustomerType.of(in.getCustomerType()));
    }
}